package com.hardy.leetcode;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * User: Ruijie Date: 2016/8/17
 */
public class FindNumber {
    public static ArrayList findNumber(){
        ArrayList<int[]> res=new ArrayList<int[]>();
        for(int num1=1;num1<=9;num1++){
            for(int num2=1;num2<=9;num2++){
                for(int num3=1;num3<=9;num3++){
                    for(int num4=1;num4<=9;num4++){
                        for(int num5=1;num5<=9;num5++){
                            if(isNotEqueal(new int[]{num1,num2,num3,num4,num5})){
                                int mulNum1=num1*10000+num2*1000+num3*100+num4*10+num5;
                                int mulNum2=num5*10000+num4*1000+num3*100+num2*10+num1;
                                for(int i=1;i<=9;i++){
                                    if(i!=num1 && i!=num2 && i!=num3 && i!=num4 && i!=num5){
                                        if(mulNum1*i==mulNum2){
                                            res.add(new int[]{num1,num2,num3,num4,num5,i});
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return res;
    }
    public static boolean isNotEqueal(int[] arr){
        for(int i=0;i<arr.length;i++){
            for(int j=i+1;j<arr.length;j++){
                if(arr[i]==arr[j]){
                    return false;
                }
            }
        }
        return true;
    }
    public static ArrayList<int[]> findNineNumber(){
        ArrayList<int[]> res=new ArrayList<>();
        for(int num1=1;num1<=9;num1++){
            for(int num2=1;num2<=9;num2++){
                for(int num3=1;num3<=9;num3++){
                    for(int num4=1;num4<=9;num4++){
                        for(int num5=1;num5<=9;num5++){
                            for(int num6=1;num6<=9;num6++){
                                for(int num7=1;num7<=9;num7++){
                                    for(int num8=1;num8<=9;num8++){
                                        for(int num9=1;num9<=9;num9++){
                                            int[] array=new int[]{num1,num2,num3,num4,num5,num6,num7,num8,num9};
                                            if(isNotEqueal(array)){
                                                int[] result=getResult(array);
                                                if(result!=null){
                                                    res.add(getResult(array));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return res;
    }

    private static int[] getResult(int[] array) {
        int num2=array[0]*10+array[1];
        int num3=array[0]*100+array[1]*10+array[2];
        int num4=array[0]*1000+array[1]*100+array[2]*10+array[3];
        int num5=array[0]*10000+array[1]*1000+array[2]*100+array[3]*10+array[4];
        int num6=array[0]*100000+array[1]*10000+array[2]*1000+array[3]*100+array[4]*10+array[5];
        int num7=array[0]*1000000+array[1]*100000+array[2]*10000+array[3]*1000+array[4]*100+array[5]*10+array[6];
        int num8=array[0]*10000000+array[1]*1000000+array[2]*100000+array[3]*10000+array[4]*1000+array[5]*100+array[6]*10+array[7];
        int num9=array[0]*100000000+array[1]*10000000+array[2]*1000000+array[3]*100000+array[4]*10000+array[5]*1000+array[6]*100+array[7]*10+array[8];
        if(num2%2==0&&num3%3==0){
            if(num4%4==0&&num5%5==0){
                if(num6%6==0&&num7%7==0){
                    if(num8%8==0&&num9%9==0){
                        return array;
                    }
                }
            }
        }
        return null;
    }

    public static void main(String[] args) {
        ArrayList<int[]> res=findNumber();
        Iterator it=res.iterator();
        while(it.hasNext()){
            int[] item= (int[]) it.next();
            for(int i=0;i<item.length;i++){
                System.out.print(item[i]+" ");
            }
            System.out.println("----------");
        }
        ArrayList<int[]> result=findNineNumber();
        Iterator its=result.iterator();
        while(its.hasNext()){
            int[] item= (int[]) its.next();
            for(int i=0;i<item.length;i++){
                System.out.print(item[i]+" ");
            }
            
            System.out.println("----------");
        }
    }
}
